/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ventasautomotrices;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Chapas
 * Esta Clase hará las conexiones directas a la base de datos
 * 
 */
public class AdminConexion {
    
  public boolean  prueba() {
    System.out.println("Prueba de conexión MySQL .");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "AutoVentas";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "pass123";
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      conn.close();
      System.out.println("Disconnected from database");
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  
  public boolean  update(String sentence) {
      
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "AutoVentas";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "pass123";
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      
      Statement st = conn.createStatement();
      
      st.executeUpdate(sentence);
      
               
      
      conn.close();
      System.out.println("Update successful");
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  
  }
  
  public boolean Query( String sentence){
  
      Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "AutoVentas";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "pass123";
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      
      Statement st = conn.createStatement();
      
      st.executeQuery(sentence);
      
             
      
      conn.close();
      System.out.println("Query Successfull");
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  public String select( String sentence){
  
      Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "AutoVentas";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "pass123";
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      
      Statement st = conn.createStatement();
      
      ResultSet rs = st.executeQuery(sentence);
      
      String str = "";
      while (rs.next()) {
        //marca modelo linea tipo color estado placa
        str += "Marca: " + rs.getNString(1)
                + " Modelo: " + rs.getNString(2)
                + " Linea: " + rs.getNString(3)
                + " Tipo: " + rs.getNString(4)
                + " Color: " + rs.getNString(5)
                + " Estado: " + rs.getNString(6)
                + " Placa: " + rs.getNString(7)
                +"\n";
      }             
      
      conn.close();
      
      System.out.println("Query Successfull");
      return str;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  
} 
    

